package com.zxtc.wh.dao;

import com.zxtc.wh.pojo.resource.ResourceReferencePojo;
import com.zxtc.wh.utils.SqlUtil;

import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 2017年4月10日16:30:33
 */
public class ResourceReferenceDaoMysqlImpl implements IResourceReference {
    @Override
    public ResourceReferencePojo create() {
        final String sqlCmd = "INSERT INTO RESOURCE_REFERENCE ()" +
                "VALUES ();";
        PreparedStatement statement = getStatement(sqlCmd);
        if (statement != null) {
            try {
                statement.executeUpdate();
                Long autoKey = SqlUtil.getAutoIndex(statement, Long.class);
                if (autoKey == null) {
                    return null;
                }
                return getInstance(autoKey);
            } catch (SQLException e) {
                onError(e);
            }
        }
        return null;
    }

    @Override
    public ResourceReferencePojo create(ResourceReferencePojo data) {
        return null;
    }

    @Override
    public ResourceReferencePojo insert(ResourceReferencePojo data) {
        final String sqlCmd = "INSERT INTO RESOURCE_REFERENCE (resource_reference_id, resource_reference_uuid, resource_reference_storage_class, resource_path, resource_file_name, resource_size, resource_hash, resource_create_timestamp, resource_reference_count)" +
                "VALUES (?,?,?,?,?,?,?,?,?);";
        PreparedStatement statement = getStatement(sqlCmd);
        if (statement != null) {
            try {
                statement.setLong(1, data.getResource_reference_id());
                statement.setString(2, data.getResource_reference_uuid());
                statement.setInt(3, data.getResource_reference_storage_class());
                statement.setString(4, data.getResource_path());
                statement.setString(5, data.getResource_file_name());
                statement.setLong(6, data.getResource_size());
                statement.setString(7, data.getResource_hash());
                statement.setDate(8, new java.sql.Date(data.getResource_create_timestamp().getTime()));
                statement.setInt(9, data.getResource_reference_count());

                statement.executeUpdate();
                return data;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public ResourceReferencePojo updateOrCreate(ResourceReferencePojo data) {
        return null;
    }

    @Override
    public Boolean isExists(Long aLong) {
        final String sqlCmd = "INSERT INTO RESOURCE_REFERENCE (resource_reference_id, resource_reference_uuid, resource_reference_storage_class, resource_path, resource_file_name, resource_size, resource_hash, resource_create_timestamp, resource_reference_count)" +
                "VALUES (?,?,?,?,?,?,?,?,?);";
        return null;
    }

    @Override
    public ResourceReferencePojo getInstance(Long id) {
        return null;
    }


    @Override
    public ResourceReferencePojo update(ResourceReferencePojo data) {
        return null;
    }

    @Override
    public void onError(Exception eMessage) {
        //打印堆栈记录
        eMessage.printStackTrace();
    }


    private PreparedStatement getStatement(String sqlCommand) {
        try {
            return SqlUtil.getInstance().getStatement(sqlCommand);
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Boolean referenceCountIncrement(ResourceReferencePojo pojo) {
        return referenceCountIncrementDecrease(pojo, true);
    }

    @Override
    public Boolean referenceCountDecrease(ResourceReferencePojo pojo) {
        return referenceCountIncrementDecrease(pojo, false);
    }

    /**
     * 引用计数自增自减
     *
     * @param pojo        要操作引用计数的记录
     * @param isIncrement 自增或是自减的命令，true：自增，false：自减
     * @return true：成功完成自增或自减，flase：没有完成自增或自减命令，但也没有报错，null：无法执行更新指令，因为抛出异常
     */
    protected Boolean referenceCountIncrementDecrease(ResourceReferencePojo pojo, boolean isIncrement) {
        //根据入参判断是自增还是自减
        final String sqlCmd = isIncrement ? "update RESOURCE_REFERENCE set resource_reference_count=resource_reference_count+1 where resource_reference_id = ?"
                : "update RESOURCE_REFERENCE set resource_reference_count=resource_reference_count-1 where resource_reference_id = ?";

        PreparedStatement statement = getStatement(sqlCmd);
        if (statement == null) {
            return null;
        }
        try {
            statement.setLong(1, pojo.getResource_reference_id());
            int i = statement.executeUpdate();
            if (i >= 1) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException e) {
            this.onError(e);
            return null;
        } finally {
            try {
                statement.close();
            } catch (SQLException e) {
                onError(e);
            }
        }
    }
}
